package com.home.cop.employee.controller;

import com.home.cop.base.BaseController;
import com.home.cop.employee.model.SysUser;
import com.home.cop.employee.service.SysUserService;
import com.home.cop.sysinfo.model.UserSession;
import com.home.cop.system.RequiredAuth;
import com.home.cop.system.Result;
import com.home.cop.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api("系统用户相关")
public class UserController extends BaseController {

    @Autowired
    private SysUserService userService;

    @PostMapping("/list")
    @RequiredAuth
    @ApiOperation(notes = "查询系统用户列表",value = "根据条件筛选系统用户列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "account", value = "账号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "instName", value = "机构名字", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态:0-启用，1-禁用", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "用户名字", required = false, dataType = "String", paramType = "query")
    })
    public Result list(@RequestParam(value = "instName",required = false) String instName,
                       @RequestParam(value = "account",required = false) String account,
                       @RequestParam(value = "status",required = false) Integer status,
                       @RequestParam(value = "name",required = false) String name){
        Map<String,Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(instName)){
            params.put("instName",instName.trim());
        }
        if (StringUtils.isNotBlank(account)){
            params.put("account",account.trim());
        }
        if (StringUtils.isNotBlank(name)){
            params.put("name",name.trim());
        }
        if (status != null){
            params.put("status",status);
        }
        return  Result.returnSuccess().setData(userService.list(params,getPageSize(),getPageNum()));
    }

    @PostMapping("/delete")
    @RequiredAuth
    @ApiOperation(notes = "删除系统用户",value = "删除系统用户")
    @ApiImplicitParams({ @ApiImplicitParam(name = "account", value = "账号", required = false, dataType = "String", paramType = "query")
    })
    public Result deleteSysUser(@RequestParam("account") String account){
        UserSession userSession = getUserSession();
        if (account.equals(userSession.getAccount())){
            return Result.returnFail().setMessage("不能删除自己！");
        }
        userService.deleteSysUser(account);
        return Result.returnSuccess().setMessage("删除成功！");
    }

    @PostMapping("/add")
    @ApiOperation(notes = "新增系统用户",value = "新增系统用户")
    @RequiredAuth
    public Result addSysUser(@RequestBody SysUser user){
        String pass = RandomStringUtils.randomAlphanumeric(10);
        user.setSysUserPasswd(MD5Utils.MD5(pass));
        return Result.returnSuccess().pushData("password",pass);
    }

    @PostMapping("/updateInfo")
    @ApiOperation(notes = "修改系统用户信息",value = "修改系统用户信息")
    @RequiredAuth
    public Result updateInfo(@RequestBody SysUser user){
        userService.updateUserInfo(user);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @PostMapping("/updateStatus")
    @ApiOperation(notes = "启用禁用用户",value = "启用禁用用户")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0-启用，1-禁用", required = true, dataType = "Integer", paramType = "query")
    })
    public Result updateStatus(@RequestParam(value = "account") String account,@RequestParam(value = "status")Integer status){
        userService.updateStatus(account,status);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @PostMapping("/pass")
    @ApiOperation(notes = "重新生成密码",value = "重新生成密码")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "String", paramType = "query")
    })
    public Result updatePass(@RequestParam(value = "account") String account){
        String pass = RandomStringUtils.randomAlphanumeric(10);
        userService.updateUserPass(account,MD5Utils.MD5(pass));
        return Result.returnSuccess().pushData("password",pass);
}
}
